using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PoolManager : MonoBehaviour
{
    //挂载所有对象池的节点
    private Transform m_PoolParents;
    //存放所有对象池的字典
    Dictionary<string, PoolBase> m_Pools = new Dictionary<string, PoolBase>();

    private void Awake()
    {
        m_PoolParents = this.transform.parent.Find("Pool");
    }

    //创建对象池
    private void CreatPool<T>(string poolName, float releaseTime) where T:PoolBase
    {
        if(!m_Pools.TryGetValue(poolName, out PoolBase pool))
        {
            GameObject go = new GameObject(poolName);
            go.transform.SetParent(m_PoolParents);
            pool = go.AddComponent<T>();
            pool.Init(releaseTime);
            m_Pools.Add(poolName, pool);
        }
    }
    //创建GameObject对象池
    public void CreatGameObjectPool(string poolName, float releaseTime)
    {
        CreatPool<GameObjectPool>(poolName, releaseTime);
    }
    //创建Asset对象池
    public void CreatAssetPool(string poolName, float releaseTime)
    {
        CreatPool<AssetPool>(poolName, releaseTime);
    }

    //回收/存储到对象池
    public void UnSpawn(string poolName, string assetName, Object asset)
    {
        if(m_Pools.TryGetValue(poolName, out PoolBase pool))
        {
            pool.UnSpwan(assetName, asset);
        }
    }
    //从对象池取出对象
    public Object Spawn(string poolName, string assetName)
    {
        if(m_Pools.TryGetValue(poolName, out PoolBase pool))
        {
            return pool.Spwan(assetName);
        }
        return null;
    }


}
